<template>
  <div class="cart-container">
    <center>  <h3>我的购物车</h3>  </center>
    <ul>

      <li v-for="item in cart" :key="item.goods_id" class="cart-item">
        <div class="item-row">
          <input type="checkbox" v-model="item.selected" @change="calculateTotalPrice">
          <div class="item-name"> 商品名称：{{ item.goods_name }} </div>
          <div class="item-description">商品描述：{{ item.goods_description }}</div>
          <div class="item-price">价格：￥{{ item.goods_price }}</div>
          <div class="item-quantity">商品数量: {{ item.count }}</div>
          <button @click="deleteItem(item.goods_id)">删除</button> <!-- 删除按钮 -->
        </div>
      </li>
    </ul>
    <center> <p>所选商品总价：￥{{ selectedTotalPrice }}</p>
      <button @click="checkout">提交订单</button>  </center>
  </div>

  <el-dialog v-model="dialogAddFormVisible" title="确认支付信息对话框" width="500">

    <div class="dialog-footer">
      <el-button @click="confirm" >确认</el-button>
      <el-button @click="confirn" >取消</el-button>
    </div>
  </el-dialog>
  <el-dialog v-model="dialog" title="支付成功" width="500">
  </el-dialog>

  <el-dialog v-model="dialon" title="支付失败" width="500">
  </el-dialog>



</template>


<script>
// import {useRouter} from "vue-router"
export default {
  data() {
    return {
      user_id:'',
      goods_id:null,
      dialogAddFormVisible:false,//新增订单信息对话框
      // router:useRouter(),
      cart:[],
      dialog:false,
      dialon:false,
      selectedTotalPrice: 0
    }
  },
  created(){
    this.user_id=localStorage.getItem("user_id")
  },
  mounted(){
    this.loadCartData();
  },
  methods: {
    loadCartData() {
      console.log(this.user_id)
      this.$http.get("http://localhost:8080/shopCart/cart",{params:{
          user_id:this.user_id
          }}).then((res)=>{
            this.cart=res.data
          console.log(this.cart)
        }).catch (function(error) {
        console.error('加载购物车数据失败:', error);
      })
    },
    confirm() {
      this.dialog = true;
    },
    confirn() {
      this.dialon = true;
    },
    deleteItem(goods_id) {
      if (confirm("确定要删除此商品吗？")) {
        this.$http.delete("http://localhost:8080/shopCart/del/",{params:{
          user_id:this.user_id,
          goods_id:this.goods_id
          }})
            .then((response) => {
              console.log(this.cart)
              this.loadCartData(); // 删除成功后重新加载购物车数据
            })
            .catch(error => {

              console.error('删除商品失败: ', error);
            });
      }
    },

    calculateTotalPrice() {
      this.selectedTotalPrice = this.cart.reduce((total, item) => {
        if (item.selected) { // 检查商品是否被选中
          return total + item.goods_price * item.count;
        }
        return total;
      }, 0);
    },
    checkout() {
      this.dialogAddFormVisible = true;

      const selectedItems = this.cart.filter(item => item.selected); // 获取选中的商品信息
      const itemIds = selectedItems.map(item => item.goods_id); // 获取选中商品的id

      this.$http.delete("http://localhost:8080/shopCart/del", {
        data: itemIds
      })
          .then(response => {
// 删除成功逻辑，重新加载商品数据等
            this.loadCartData();
          })
          .catch(error => {
            console.error('删除商品失败: ', error);
          });
    },

  }
}

</script>

<style>
.item-row {
  display: flex; /* 使用 flex 布局 */
  align-items: center; /* 沿交叉轴居中对齐 */
}
/* 基础样式 */
.cart-container {
  max-width: 800px;
  margin: 20px auto;
  padding: 20px;
  border: 1px solid #ddd;
  border-radius: 5px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.cart-item {
  list-style: none;
  margin-bottom: 10px;
  padding: 10px;
  background-color: #f5f5f5;
  border-radius: 5px;
  display: flex;
  align-items: center;
}



/* 表格化布局 */
.item-row p {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 5px;
}

.item-row p span {
  display: inline-block;
  margin-right: 10px;
}

/* 复选框样式 */
.cart-item input[type="checkbox"] {
  margin-right: 5px;
}

/* 总价样式 */
p.total-price {
  font-weight: bold;
  margin-top: 20px;
}

/* 按钮样式 */
button {
  padding: 10px 15px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 15px;
}

button.checkout-btn {
  background-color: #79bbff;
  color: white;
  border: none;
}

/* 对话框样式 */
.el-dialog {
  text-align: left;
}

.el-dialog__header {
  padding: 10px 20px;
  background-color: #f5f5f5;
  border-bottom: 1px solid #ddd;
  border-radius: 5px 5px 0 0;
}

.el-dialog__footer {
  padding: 10px 20px;
  background-color: #f5f5f5;
  border-top: 1px solid #ddd;
  border-radius: 0 0 5px 5px;
  display: flex;
  justify-content: flex-end;
}

.el-button {
  margin-left: 10px;
}

.el-button--primary {
  background-color: #40a9ff;
  color: white;
  border: none;
}

/* 修改商品名称和描述的样式 */
.item-row p {
  display: flex;
  align-items: center;
  margin-bottom: 5px;
}

/* 商品名称样式 */
.item-name {
  font-size: 18px;
  font-weight: bold;
}

/* 商品描述样式 */
.item-description {
  font-size: 14px;
  color: #666;
}

/* 商品数量样式 */
.item-quantity {
  font-size: 16px;
}

/* 修改按钮样式 */
button {
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
  background-color: #79bbff;
  color: white;
  border: none;
}


</style>